// pages/shimingrenzheng/shimingrenzheng.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    xinshu:[
      {id:'',name:"请输入公司名称",credit_no:"请输入税号",start_date:"请输入姓名"},
    ],
    cha_s:'6',
    xs_2:"display:block",
    xian:"display:none",
    xs_3:"display:none",
    xs_4:"display:none",
    xs:"display:none",
    yy_dxs:"display:none",
    tempFilePaths1:"../../images/sf_img1.jpg",
    tempFilePaths2:"../../images/sf_img2.jpg",
    tempFilePaths3:"../../images/sf_img8.jpg",
    tempFilePaths33:"../../images/sf_img8.jpg",
    yc_s:"display:none",
    count:60,
    code:'获取验证码',
    iphoneValue:' ', //手机号码
    inputShowed:"flase",
    inputShowed1:"flase",
    inputShowed2:"flase",
    inputShowed3:"flase",
    inputShowed4:"flase",
    inputShowed5:"flase",
    yz:"0",
    
    statusBarHeight1: getApp().globalData.statusBarHeight1,
  },
  chooseimage1: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9  
      // 可以指定是原图还是压缩图，默认二者都有  
      sizeType: ['original', 'compressed'],
      // 可以指定来源是相册还是相机，默认二者都有
      sourceType: ['album', 'camera'],
      // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片   
      success: function (res) {
        //前台显示
        
         console.log(res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths1 = res.tempFilePaths
        that.setData({
          tempFilePaths1: res.tempFilePaths
        })
        wx.showLoading({
          title: '上传中...',
          mask: true,
        })
        const uploadTask =  wx.uploadFile({
          url: 'https://shy.xihenet.net/api/index/order_img',
          filePath: tempFilePaths1[0],
          name: 'file',
          header: {
            "content-type": "multipart/form-data",
            "content-type": "application/x-www-form-urlencoded"
          },
          success: function (res) {
            //打印
            console.log(res)
            app.globalData.file1 = res.data
            var str = app.globalData.file1;
            app.globalData.file3 = str.replace(/^\"|\"$/g, '');
            console.log(app.globalData.file3)
            console.log(str.replace(/^\"|\"$/g, ''))
            
            wx.getFileSystemManager().readFile({
              filePath: tempFilePaths1[0],
              encoding:"base64",
              success: function (data){
                console.log(data)//返回base64编码结果，但是图片的话没有data:image/png
                wx.showLoading({
                  title: '加载中...',
                  mask: true,
                })
                 wx.request({
                    url: 'https://apis.juhe.cn/idimage/verify?key=0b9429b323f9645b81bb111ff1bb804b',
                    method: 'post',
                    data:{
                      image:data.data,
                      side:1
                    },
                    header: {
                      'content-type': 'application/x-www-form-urlencoded'
                    },
                    success: function (res) {
                      console.log(res)
                      app.globalData.kp_name = res.data.result.realname
                      app.globalData.xingming  = res.data.result.realname
                      app.globalData.sf_z = res.data.result.idcard
                      app.globalData.shenfen=  res.data.result.idcard
                      if(app.globalData.kp_name!=''&&app.globalData.kp_name!=null&&app.globalData.kp_name!=undefined&&app.globalData.sf_z!=''&&app.globalData.sf_z!=null&&app.globalData.sf_z!=undefined){
                        console.log(app.globalData.begin)
                        console.log(app.globalData.end)
                        if(app.globalData.begin==""||app.globalData.begin==null||app.globalData.begin==undefined){

                        }else if(app.globalData.end==""||app.globalData.end==null||app.globalData.end==undefined){
                         
                        }else{
                          console.log(1111)
                         wx.request({
                           url: 'https://apis.juhe.cn/id_card_four_factors/query?key=a27670eb14416e7586d180ecc33df39d',
                           method: 'post',
                           data:{
                             realname:app.globalData.kp_name,
                             idcard:app.globalData.sf_z,
                             start_date:app.globalData.begin,
                             end_date:app.globalData.end
                           },
                           header: {
                             'content-type': 'application/x-www-form-urlencoded'
                           },
                           success: function (res) {
                             console.log(res)
                             if(res.data.result.res==1){
                                 app.globalData.byz = 1
                                
                             }else{
                              app.globalData.byz = 2
                             }
                           }
                         })
                        }
                      }
                  
                      if(res.data.result.realname==''||res.data.result.realname==null||res.data.result.realname==undefined){
                        that.setData({
                          xs1_1:"display:none",
                          xs1_2:"display:block",
                         })
                        wx.showModal({
                          title: '',
                          content: '请确认身份证正面是否正确',
                          showCancel: false,
                          success:function(res){
                                if(res.confirm){
                                 
                                }else{
                                   
                                }
                          }
                        })
                      }else if(res.data.result.idcard==''||res.data.result.idcard==null||res.data.result.idcard==undefined){
                        wx.showModal({
                          title: '',
                          content: '请确认身份证正面是否正确',
                          showCancel: false,
                          success:function(res){
                                if(res.confirm){
                                 
                                }else{
                                   
                                }
                          }
                        })
                      }
                        if(res.data.result.realname==''||res.data.result.realname==null||res.data.result.realname==undefined){
                          that.setData({
                            xs1_2:"display:block",
                            xs1_1:"display:none",
                           })
                        }else{
                          that.setData({
                            xs1_1:"display:block",
                            xs1_2:"display:none",
                           })
                        }
                        if(res.data.result.idcard==''||res.data.result.idcard==null||res.data.result.idcard==undefined){
                          that.setData({
                            xs1_4:"display:block",
                            xs1_3:"display:none",
                           })
                       }else{
                        that.setData({
                          xs1_3:"display:block",
                          xs1_4:"display:none",
                         })
                       }
                     
                        that.setData({
                          sf_name:res.data.result.realname,
                          sf_hm:res.data.result.idcard
                       })
                      
                    }
                  })
                  uploadTask.onProgressUpdate((res) => {
                    console.log('上传进度', res.progress)
                    if(res.progress=='100'){
                      setTimeout(function () {
                        wx.hideLoading()   //隐藏loading
                      }, 1000)
                    }
                    console.log('已经上传的数据长度', res.totalBytesSent)
                    console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
                  })
                  setTimeout(function () {
                    wx.hideLoading()   //隐藏loading
                  }, 1000)
              }
            })
          }, fail: function (err) {
          }
        })
      }
    })
  },

  chooseimage2: function (e) {
    var that = this;
    if(app.globalData.file3==null||app.globalData.file3==""||app.globalData.file3==undefined){
      wx.showModal({
        title: '',
        content: '请先上传身份正面图片',
        showCancel: false,
        success:function(res){
              if(res.confirm){
               
              }else{
                 
              }
        }
      })
    }else{
      wx.chooseImage({
        count: 1, // 默认9  
        // 可以指定是原图还是压缩图，默认二者都有  
        sizeType: ['original', 'compressed'],
        // 可以指定来源是相册还是相机，默认二者都有
        sourceType: ['album', 'camera'],
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片   
        success: function (res) {
          //前台显示
          that.setData({
            tempFilePaths2: res.tempFilePaths
          })
  
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths2 = res.tempFilePaths
          wx.showLoading({
            title: '上传中...',
            mask: true,
          })
          const uploadTask =  wx.uploadFile({
            url: 'https://shy.xihenet.net/api/index/order_img',
            filePath: tempFilePaths2[0],
            name: 'file',
            header: {
              "content-type": "multipart/form-data",
              "content-type": "application/x-www-form-urlencoded"
            },
            success: function (res) {
              //打印
              app.globalData.file2 = res.data
              var str = app.globalData.file2;
              str.replace(/^\"|\"$/g, '');
              app.globalData.file4 = str.replace(/^\"|\"$/g, '');
              that.setData({
                tempFilePaths2: "https://shy.xihenet.net/uploads/image/"+app.globalData.file4
              })
              wx.getFileSystemManager().readFile({
                filePath: tempFilePaths2[0],
                encoding:"base64",
                success: function (data){
                  console.log(data)//返回base64编码结果，但是图片的话没有data:image/png
                  wx.showLoading({
                    title: '加载中...',
                    mask: true,
                  })
                   wx.request({
                      url: 'https://apis.juhe.cn/idimage/verify?key=0b9429b323f9645b81bb111ff1bb804b',
                      method: 'post',
                      data:{
                        image:data.data,
                        side: "back"
                      },
                      header: {
                        'content-type': 'application/x-www-form-urlencoded'
                      },
                      success: function (res) {
                        console.log(res)
                        app.globalData.begin = res.data.result.begin
                        app.globalData.end = res.data.result.end
                        if(res.data.result.begin==''||res.data.result.begin==null||res.data.result.begin==undefined){
                          that.setData({
                            xs1_6:"display:block",
                            xs1_5:"display:none",
                           })
                          wx.showModal({
                            title: '',
                            content: '请确认身份证反面是否正确',
                            showCancel: false,
                            success:function(res){
                                  if(res.confirm){
                                   
                                  }else{
                                     
                                  }
                            }
                          })
                        }else if(res.data.result.end==''||res.data.result.end==null||res.data.result.end==undefined){
                          that.setData({
                            xs1_8:"display:block",
                            xs1_7:"display:none",
                           })
                          wx.showModal({
                            title: '',
                            content: '请确认身份证反面是否正确',
                            showCancel: false,
                            success:function(res){
                                  if(res.confirm){
                                   
                                  }else{
                                     
                                  }
                            }
                          })
                        }else{
                          if(res.data.result.begin==''||res.data.result.begin==null||res.data.result.begin==undefined){
                            that.setData({
                              xs1_6:"display:block",
                              xs1_5:"display:none",
                             })
                         }else{
                          that.setData({
                            xs1_5:"display:block",
                            xs1_6:"display:none",
                           })
                         }
                         if(res.data.result.end==''||res.data.result.end==null||res.data.result.end==undefined){
                          that.setData({
                            xs1_8:"display:block",
                            xs1_7:"display:none",
                           })
                        }else{
                          that.setData({
                            xs1_7:"display:block",
                            xs1_8:"display:none",
                           })
                        }

                          if(res.data.reason=="成功"){
                            wx.request({
                              url: 'https://apis.juhe.cn/id_card_four_factors/query?key=a27670eb14416e7586d180ecc33df39d',
                              method: 'post',
                              data:{
                                realname:app.globalData.kp_name,
                                idcard:app.globalData.sf_z,
                                start_date:app.globalData.begin,
                                end_date:app.globalData.end
                              },
                              header: {
                                'content-type': 'application/x-www-form-urlencoded'
                              },
                              success: function (res) {
                                 console.log(res)
                                 if(res.data.result.res==1){
                                     app.globalData.byz = 1
                                    
                                 }else{
                                  app.globalData.byz = 2
                                 }
                              }
                            })
                          //   that.setData({
                          //     sf_name:res.data.result.realname,
                          //     sf_hm:res.data.result.idcard
                          //  })
                          }
                        }
                     
                      }
                    })
                    uploadTask.onProgressUpdate((res) => {
                      console.log('上传进度', res.progress)
                      if(res.progress=='100'){
                        setTimeout(function () {
                          wx.hideLoading()   //隐藏loading
                        }, 1000)
                      }
                      console.log('已经上传的数据长度', res.totalBytesSent)
                      console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
                    })
                    setTimeout(function () {
                      wx.hideLoading()   //隐藏loading
                    }, 1000)
                }
              })
            }, fail: function (err) {
            }
          })
        }
      })
    }
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  sm_rz:function(){
    var that = this;
    if(app.globalData.file3==null||app.globalData.file3==''||app.globalData.file3==undefined){
      wx.showModal({
        title: '',
        content: '请上传身份证人像照',
        showCancel: false,
        success:function(res){
              if(res.confirm){
               
              }else{
                 
              }
        }
      })
    }else  if(app.globalData.file4==null||app.globalData.file4==''||app.globalData.file4==undefined){
      wx.showModal({
        title: '',
        content: '请上传身份证国徽照',
        showCancel: false,
        success:function(res){
              if(res.confirm){
               
              }else{
                 
              }
        }
      })
    }
    // else  if(app.globalData.kp_name==''||app.globalData.kp_name==null||app.globalData.kp_name==undefined){
    //   console.log(app.globalData.kp_name)
    //    wx.showModal({
    //      title: '',
    //      content: '请确认身份证人像照是否正确',
    //      showCancel: false,
    //      success:function(res){
    //            if(res.confirm){
                
    //            }else{
                  
    //            }
    //      }
    //    })
    //  }else if(app.globalData.sf_z==''||app.globalData.sf_z==null||app.globalData.sf_z==undefined){
    //    console.log(app.globalData.idcard)
    //    wx.showModal({
    //      title: '',
    //      content: '请确认身份证人像照是否正确',
    //      showCancel: false,
    //      success:function(res){
    //            if(res.confirm){
                
    //            }else{
                  
    //            }
    //      }
    //    })
    //  }else  if(app.globalData.begin==''||app.globalData.begin==null||app.globalData.begin==undefined){
    //   wx.showModal({
    //     title: '',
    //     content: '请确认身份证国徽照是否正确',
    //     showCancel: false,
    //     success:function(res){
    //           if(res.confirm){
               
    //           }else{
                 
    //           }
    //     }
    //   })
    // }else if(app.globalData.end==''||app.globalData.end==null||app.globalData.end==undefined){
    //   wx.showModal({
    //     title: '',
    //     content: '请确认身份证国徽照是否正确',
    //     showCancel: false,
    //     success:function(res){
    //           if(res.confirm){
               
    //           }else{
                 
    //           }
    //     }
    //   })
    // } 
    else if(app.globalData.byz==2){
      wx.showModal({
        title: '',
        content: '身份证四要素不一致',
        showCancel: false,
        success:function(res){
              if(res.confirm){
               
              }else{
                 
              }
        }
      })
    }else if(app.globalData.xingming==''||app.globalData.xingming==null||app.globalData.xingming==undefined){
      wx.showToast({
        title: '请输入姓名!',
        icon: 'none',
        duration: 2000
      })
      that.setData({
        inputShowed:"true"
      })
    }else if(app.globalData.shenfen==''||app.globalData.shenfen==null||app.globalData.shenfen==undefined){
      wx.showToast({
        title: '请输入身份证号!',
        icon: 'none',
        duration: 2000
      })
      that.setData({
        inputShowed1:"true"
      })
    }else if(app.globalData.shouji==''||app.globalData.shouji==null||app.globalData.shouji==undefined){
      wx.showToast({
        title: '请输入手机号!',
        icon: 'none',
        duration: 2000
      })
      that.setData({
        inputShowed2:"true"
      })
    }else if(app.globalData.youxiang==''||app.globalData.youxiang==null||app.globalData.youxiang==undefined){
      // wx.showModal({
      //   title: '',
      //   content: '请输入邮箱内容!',
      //   showCancel: false,
      //   success:function(res){
      //         if(res.confirm){
               
      //         }else{
                 
      //         }
      //   }
      // })
      wx.showToast({
        title: '请输入邮箱内容!',
        icon: 'none',
        duration: 2000
      })
      that.setData({
        inputShowed4:"true"
      })
    }else if(app.globalData.youxiang.search("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$") == -1){
      wx.showModal({
        title: '',
        content: '邮箱格式错误!',
        showCancel: false,
        success:function(res){
              if(res.confirm){
               
              }else{
                 
              }
        }
      })
      return false;  
    }else{
      wx.request({
        url: app.globalData.url_l + 'api/member/shiming',
        method: 'post',
        data:{
            uid:app.globalData.uid,
            shen_zheng:app.globalData.file3,
            shen_fan:app.globalData.file4,
            name:app.globalData.xingming,
            mobile:app.globalData.shouji,
            idnum:app.globalData.shenfen,
            email:app.globalData.youxiang,
            yanzhengma:app.globalData.yanzheng
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          console.log(res)
          if(res.data.code==1){
            wx.showToast({
              title: '实名认证成功！',
              icon: 'none',
              duration: 2000,
              success:function(){
                setTimeout(function () {
                  //要延时执行的代码
                  wx.navigateBack()
                  that.setData({
                    yc_s:"display:block"
                  })
                  
                }, 2000) //延迟时间
              }
            })
           
          }else if(res.data.code==7){
            
          app.globalData.zx_id = res.data.zx_id
            wx.showModal({
              title: '温馨提示',
              content:  res.data.msg,
              success (res) {
                if (res.confirm) {
                  wx.request({
                    url: app.globalData.url_l + 'api/member/zx_shen',
                    method: 'post',
                    data:{
                      zx_id:app.globalData.zx_id,
                    },
                    header: {
                      'content-type': 'application/x-www-form-urlencoded'
                    },
                    success: function (res) {
                      console.log(res)
                      if(res.data.code==1){
                        wx.showToast({
                          title: res.data.msg,
                          icon: 'none',
                          duration: 2000
                        })
                      }else{
                        wx.showToast({
                          title: res.data.msg,
                          icon: 'none',
                          duration: 2000
                        })
                      }
                    }
                  })
                  
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }else if(res.data.code==6){
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }else{
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
    
  },
  // 验证码按钮
  yanzheng1:function(e){
    let that = this
  // 手机号码格式验证
  this.data.yz++
  console.log(this.data.yz)
  if (!(/^1[3456789]\d{9}$/.test(app.globalData.shouji))) {
    this.data.yz=0
    wx.showToast({
      title: '输入手机号有误',
      icon: 'none',
      duration: 2000
    })
    return;
  }
  if(that.data.code !== '获取验证码'){
    return
  }
  if(this.data.yz=='1'){
    const countDown = setInterval(() => {
      if(that.data.count <= 0){
        that.setData({
          count:60,
          code:'获取验证码'
        })
        clearInterval(countDown)
          return
        }
        that.data.count --
        if(that.data.count==0){
          this.data.yz=0
        }
        that.setData({
          count: that.data.count,
          code: that.data.count < 10 ? `请等待0${that.data.count}s` : `请等待${that.data.count}s`
        })
      },1000);
  }
 
  // 调用验证码接口
 // that.textCode_http()
    wx.request({
      url: app.globalData.url_l + 'api/index/yanzhengma',
      method: 'post',
      data:{
        mobile:app.globalData.shouji,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 1000
        })
      
      }
    })
  },
  // 验证码input
  yanzheng:function(e){
    app.globalData.yanzheng = e.detail.value
  },
  // 邮箱
  youxiang:function(e){
    var that = this;
    app.globalData.youxiang = e.detail.value
    console.log(app.globalData.youxiang)
    that.setData({
      youxiang:e.detail.value
    })
  },
  // 手机号
  shouji:function(e){
    app.globalData.shouji = e.detail.value
  },
  // 身份号
  shenfen:function(e){
    app.globalData.shenfen = e.detail.value
  },
  // 姓名
  xingming:function(e){
    app.globalData.xingming = e.detail.value
  },
  zhidao:function(e){
     var that = this;
     that.setData({
      yc_s:"display:none"
     })
     wx.navigateBack()
  },
  onLoad: function (options) {
    var that = this;
    wx.request({
      url: app.globalData.url_l + 'api/member/gai',
      method: 'post',
      data:{
        uid:app.globalData.uid,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
         console.log(res)
       
            
         if(res.data.shen_zheng==''||res.data.shen_zheng=='null'||res.data.shen_zheng=='undefined'){
          app.globalData.file3 = ''
             that.setData({
              tempFilePaths1:"../../images/sf_img1.jpg"
             })
         }else{
          app.globalData.byz = 1
          app.globalData.file3 = res.data.shen_zheng
            that.setData({
              tempFilePaths1:"https://shy.xihenet.net/uploads/image/"+res.data.shen_zheng
            })
         }
         if(res.data.shen_fan==''||res.data.shen_fan=='null'||res.data.shen_fan=='undefined'){
          app.globalData.file4 = ''
              that.setData({
                tempFilePaths2:"../../images/sf_img2.jpg"
              })
          }else{
            app.globalData.file4 = res.data.shen_fan
            that.setData({
              tempFilePaths2:"https://shy.xihenet.net/uploads/image/"+res.data.shen_fan
            })
          }
          if(res.data.name==''||res.data.name=='null'||res.data.name=='undefined'){
            app.globalData.xingming = ''
                that.setData({
                  sf_name:res.data.name,
                  sf_hm:res.data.idnum
                })
            }else{
              app.globalData.xingming = res.data.name
              that.setData({
                sf_name:res.data.name,
                sf_hm:res.data.idnum
              })
            }
            if(res.data.idnum==''||res.data.idnum=='null'||res.data.idnum=='undefined'){
              app.globalData.shenfen = ''
                  that.setData({
                    sf_name:res.data.name,
                    sf_hm:res.data.idnum
                  })
              }else{
                app.globalData.shenfen = res.data.idnum
                that.setData({
                  sf_name:res.data.name,
                  sf_hm:res.data.idnum
                })
              }
              if(res.data.mobile==''||res.data.mobile=='null'||res.data.mobile=='undefined'){
                app.globalData.shouji = ''
                    that.setData({
                      mobile:res.data.mobile,
                    })
                }else{
                  app.globalData.shouji = res.data.mobile
                  that.setData({
                    mobile:res.data.mobile,
                  })
                }
                if(res.data.email==''||res.data.email=='null'||res.data.email=='undefined'){
                  app.globalData.youxiang = ''
                      that.setData({
                        email:res.data.email,
                      })
                  }else{
                    app.globalData.youxiang = res.data.email
                    that.setData({
                      email:res.data.email,
                    })
                  }
                  app.globalData.yanzheng = ''
         that.setData({
           sm_nr:res.data
         })
      }
    })
    this.setData({
      navH: app.globalData.navHeight,
    })
  },
  fanhui:function(e){
   wx.switchTab({
     url: '../wode/wode',
   })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})